package com.per.mybootall.htmlToPdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.UUID;

/**
 * wkhtmltopdf工具类
 *
 * 约定：
 *      1. 插件安装位置，在Windows系统中将插件安装在D盘根目录下（D:/）, 在Linux系统中将插件安装在opt目录下（/opt）
 *
 * 注意：
 *      1. wkhtmltopdf的Linux版本中，解压后，默认的文件名为"wkhtmltox"，为了统一起见，一律将解压后的文件名，重命名为"wkhtmltopdf"（命令：mv wkhtmltox wkhtmltopdf）
 *
 * Created by kagome on 2016/7/26.
 */
public class html2pdf {
    // 临时目录的路径
    public static final String TEMP_DIR_PATH = html2pdf.class.getResource("/").getPath().substring(1) + "temp/";
    //public static final String TEMP_DIR_PATH = "D:\\"+ "temp\\";
    static {
        // 生成临时目录
        new File(TEMP_DIR_PATH).mkdirs();
        System.out.println("======:"+TEMP_DIR_PATH);
    }

    public static void main(String[] args) throws Exception {
        htmlToPdf("D:\\1615426761-NY0F1ONVI2.html",  "D:\\1.pdf");
    }

    /**
     * 将HTML文件内容输出为PDF文件
     *
     * @param htmlFilePath HTML文件路径
     * @param pdfFilePath  PDF文件路径
     */
    public static void htmlToPdf(String htmlFilePath, String pdfFilePath) {
        try {
            Process process = Runtime.getRuntime().exec(getCommand(htmlFilePath, pdfFilePath));
            new Thread(new ClearBufferThread(process.getInputStream())).start();
            new Thread(new ClearBufferThread(process.getErrorStream())).start();
            process.waitFor();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 将HTML字符串转换为HTML文件
     *
     * @param htmlStr HTML字符串
     * @return HTML文件的绝对路径
     */
    public static String strToHtmlFile(String htmlStr) {
        OutputStream outputStream = null;
        try {
            String htmlFilePath = TEMP_DIR_PATH + UUID.randomUUID().toString() + ".html";
            outputStream = new FileOutputStream(htmlFilePath);
            outputStream.write(htmlStr.getBytes("UTF-8"));
            return htmlFilePath;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                    outputStream = null;
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 获得HTML转PDF的命令语句
     *
     * @param htmlFilePath HTML文件路径
     * @param pdfFilePath  PDF文件路径
     * @return HTML转PDF的命令语句
     */
    private static String getCommand(String htmlFilePath, String pdfFilePath) {
        String osName = System.getProperty("os.name");
        // Windows
        if (osName.startsWith("Windows")) {
            return String.format("D:\\wkhtmltopdf\\bin\\wkhtmltopdf.exe %s %s", htmlFilePath, pdfFilePath);
        }
        // Linux
        else {
            return String.format("/opt/wkhtmltopdf/bin/wkhtmltopdf %s %s", htmlFilePath, pdfFilePath);
        }
    }

}
